﻿using Itech_Run.Application.Shared;
using Itech_Run.Blog.Application.Contracts.Dto;
using Itech_Run.Blog.Application.Contracts.Service;
using Itech_Run.Blog.Domain.Entity;
using System.Linq.Expressions;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Itech_Run.Blog.Application.Service
{
    public class Front_BlogCommentService : BaseApplicationService<Blog_Comment>, IFront_BlogCommentService
    {
        private readonly IRepository<Blog_Comment, string> _blogCommentRepository;
        public Front_BlogCommentService(IRepository<Blog_Comment, string> blogCommentRepository)
        {
            _blogCommentRepository = blogCommentRepository;
        }
        public async Task<PagedResultDto<Front_BlogCommentDto>> GetListAsync(Front_BlogCommentListDto listDto)
        {
            if (string.IsNullOrEmpty(listDto.Sorting)) listDto.Sorting = "CreatedTime desc";
            Expression<Func<Blog_Comment, bool>> curExpression = s => s.IsChecked == true && s.BlogId == listDto.BlogId;

            var query = (await _blogCommentRepository.WithDetailsAsync()).WhereIf(true, curExpression).OrderBy(listDto.Sorting);
            var items = ObjectMapper.Map<List<Blog_Comment>, List<Front_BlogCommentDto>>(query.PageBy(listDto.SkipCount, listDto.MaxResultCount).ToList());

            return new PagedResultDto<Front_BlogCommentDto>(query.Count(), items);
        }

        public async Task InsertAnonymousAsync(BlogCommentCreateDto dto)
        {
            var blogComment = ObjectMapper.Map<BlogCommentCreateDto, Blog_Comment>(dto);
            SetInsertBy(blogComment, null);
            blogComment.IsAnonymous = true;
            blogComment.IsChecked = false;
            await _blogCommentRepository.InsertAsync(blogComment);
        }
    }
}
